package com.zch.multiple;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
 * The type Multiple data source.
 *
 * @author zchcpywin10
 */
public class MultipleDataSource extends AbstractRoutingDataSource {
  private static final ThreadLocal<String> CONTEXT_HOLDER = new InheritableThreadLocal<>();
  @Override
  protected Object determineCurrentLookupKey() {
    // 不应该通过类实例访问静态成员 com.ssm.multiple.MultipleDataSource.getDataSource()
    // return this.getDataSource()
    return getDataSource();
  }
  /**
   * 设置数据源
   *
   * @param db the db
   */
  public static void setDataSource(String db) {
    CONTEXT_HOLDER.set(db);
  }
  /**
   * 取得当前数据源
   *
   * @return data source
   */
  public static String getDataSource() {
    return CONTEXT_HOLDER.get();
  }
  /**
   * 清除上下文数据
   */
  public static void clear() {
    CONTEXT_HOLDER.remove();
  }
}